package uvt.map;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.openstreetmap.gui.jmapviewer.Coordinate;

import uvt.gui.mainframe.CreateTransportationNetworkPanel;
import uvt.map.graph.element.Segment;
import uvt.map.graph.element.Station;
import uvt.map.graph.element.Vehicle;
import uvt.map.mapmarker.MapMarkerDot;
import uvt.map.mapmarker.MapPolygonImpl;

public class PositionsSettings {
	
	private static PositionsSettings instance;
	
	private JMapViewer map;
	private JTextField txtMyPosLat;
	private JTextField txtMyPosLon;
	private JTextField txtArrivePosLat;
	private JTextField txtArrivePosLon;
	private JTextField txtSegmentLatitude;
	private JTextField txtSegmentLongitude;
	private JRadioButton rdbMyPos;
	private JRadioButton rdbArrivePos;
	
	private static MapMarkerDot myPosDot;
    private static MapMarkerDot myArriveDot;
    private static MapMarkerDot posDot;
	
	private PositionsSettings() {
	}
	
	public static PositionsSettings getInstance() {
		if(instance == null)
			instance = new PositionsSettings();
		return instance;
	}
	
	public JMapViewer getMap() {
		return map;
	}

	public void setMap(JMapViewer map) {
		this.map = map;
	}

	public JTextField getTxtMyPosLat() {
		return txtMyPosLat;
	}
	
	public void setTxtMyPosLat(JTextField txtMyPosLat) {
		this.txtMyPosLat = txtMyPosLat;
	}
	
	public JTextField getTxtMyPosLon() {
		return txtMyPosLon;
	}
	
	public void setTxtMyPosLon(JTextField txtMyPosLon) {
		this.txtMyPosLon = txtMyPosLon;
	}
	
	public JTextField getTxtArrivePosLat() {
		return txtArrivePosLat;
	}
	
	public void setTxtArrivePosLat(JTextField txtArrivePosLat) {
		this.txtArrivePosLat = txtArrivePosLat;
	}
	
	public JTextField getTxtArrivePosLon() {
		return txtArrivePosLon;
	}
	
	public void setTxtArrivePosLon(JTextField txtArrivePosLon) {
		this.txtArrivePosLon = txtArrivePosLon;
	}
	
	public JTextField getTxtSegmentLatitude() {
		return txtSegmentLatitude;
	}

	public void setTxtSegmentLatitude(JTextField txtSegmentLatitude) {
		this.txtSegmentLatitude = txtSegmentLatitude;
	}

	public JTextField getTxtSegmentLongitude() {
		return txtSegmentLongitude;
	}

	public void setTxtSegmentLongitude(JTextField txtSegmentLongitude) {
		this.txtSegmentLongitude = txtSegmentLongitude;
	}

	public JRadioButton getRdbMyPos() {
		return rdbMyPos;
	}
	
	public void setRdbMyPos(JRadioButton rdbMyPos) {
		this.rdbMyPos = rdbMyPos;
	}
	
	public JRadioButton getRdbArrivePos() {
		return rdbArrivePos;
	}
	
	public void setRdbArrivePos(JRadioButton rdbArrivePos) {
		this.rdbArrivePos = rdbArrivePos;
	}
	
	public void coordinateSettingsBehaviourMapPanel(Coordinate coord) {
		if(rdbMyPos.isSelected()) {
			txtMyPosLat.setText(String.valueOf(coord.getLat()));
			txtMyPosLon.setText(String.valueOf(coord.getLon()));
			map.removeMapMarker(myPosDot);
			MapMarkerDot.setDotColor(Color.GREEN);
			myPosDot = new MapMarkerDot("My Position",coord,8 + map.getZoomSlider().getValue() - 19);
			map.addMapMarker(myPosDot);
		} else if(rdbArrivePos.isSelected()) {
			txtArrivePosLat.setText(String.valueOf(coord.getLat()));
			txtArrivePosLon.setText(String.valueOf(coord.getLon()));
			map.removeMapMarker(myArriveDot);
			MapMarkerDot.setDotColor(Color.RED);
			myArriveDot = new MapMarkerDot("Arrive Position",coord,8 + map.getZoomSlider().getValue() - 19);
			map.addMapMarker(myArriveDot);
		}
	}
	
	public void coordinateSettngsBehaviourSSSPanel(Coordinate coord) {
		if(CreateTransportationNetworkPanel.isSSSPanelSelected()) {
			txtSegmentLatitude.setText(String.valueOf(coord.getLat()));
			txtSegmentLongitude.setText(String.valueOf(coord.getLon()));
			map.removeMapMarker(posDot);
			MapMarkerDot.setDotColor(Color.white);
			posDot = new MapMarkerDot("",coord,8 + map.getZoomSlider().getValue() - 19);
			map.addMapMarker(posDot);
		}
	}
	
	public void cleanMapMarkersUsed() {
		map.removeMapMarker(myPosDot);
		map.removeMapMarker(myArriveDot);
		map.removeMapMarker(posDot);
    }
	
	public void cleanMap() {
		map.removeAllMapMarkers();
		map.removeAllMapPolygons();
	}
	
	public void addStationOnMap(Station station) {
		map.addMapMarker(new MapMarkerDot(station.getName(),
                                          new Coordinate(station.getLatitude(),
        		                                         station.getLongitude()),
        		                          8 + map.getZoomSlider().getValue() - 19)); 
	}
	
	public void addSegmentOnMap(String name,Segment seg) {
		ArrayList<Coordinate> coordList = new ArrayList<Coordinate>();
    	coordList.add(new Coordinate(seg.getxA(),seg.getyA()));
    	coordList.add(new Coordinate(seg.getxA(),seg.getyA()));
    	coordList.add(new Coordinate(seg.getxB(),seg.getyB()));
    	coordList.add(new Coordinate(seg.getxB(),seg.getyB()));
		map.addMapPolygon(new MapPolygonImpl(name,coordList));
	}
	
	public void addVehicleOnMap(Vehicle v,Coordinate vehicleCoord) {
		map.addMapMarker(new MapMarkerDot("Vehicle " + v.getName(),
									      vehicleCoord,
									      8 + map.getZoomSlider().getValue() - 19));
	}
	
}